package com.msblearn.C_014;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 并行流：底层也是并行流
 */
public class T09_ParallelStreamAPI {
    public static void main(String[] args) {
        List<Integer> integerList = new ArrayList<>();
        Random r = new Random();
        for (int i = 0; i < 10000; i++) {
            integerList.add(1000000 + r.nextInt(100000));
        }

        long start = System.currentTimeMillis();
        integerList.forEach(v -> {
            isPrime(v);
        });
        long end = System.currentTimeMillis();
        System.out.println(end - start);
        //使用parallelStream
        start = System.currentTimeMillis();
        integerList.parallelStream().forEach(T09_ParallelStreamAPI::isPrime);
        end = System.currentTimeMillis();
        System.out.println(end - start);
    }

    static boolean isPrime(int num) {
        for (int i = 2; i < num / 2; i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }
}
